
#This script replicates all tables and figures in the main manuscript for "Participatory Unilateralism"

#Load data
load("replication_data.Rdata")

#Load packages 
pacman::p_load(tidyverse, stargazer, modelsummary, fixest, MASS, kableExtra, ggplot2, ggeffects)



#Table 2: Sample Distribution of Delegation and Discretion Scores ----
table2 <-
    rbind(c("", "Low Delegation", "High Delegation", "Total"),
          c("Low Discretion", nrow(eo.df[eo.df$hidis == 0 & eo.df$hidel == 0,]), nrow(eo.df[eo.df$hidis == 0 & eo.df$hidel == 1,]), nrow(eo.df[eo.df$hidis == 0,])),
          c("High Discretion", nrow(eo.df[eo.df$hidis == 1 & eo.df$hidel == 0,]), nrow(eo.df[eo.df$hidis == 1 & eo.df$hidel == 1,]), nrow(eo.df[eo.df$hidis == 1,])),
          c("Total", nrow(eo.df[eo.df$hidel == 0,]), nrow(eo.df[eo.df$hidel == 1,]), nrow(eo.df)))

capture.output(stargazer(table2,
                         title = "Sample Distributrion of Delegation and Discretion Scores",
                         type = 'text',
                         header = FALSE),
               file = "./table2.txt")

#Table 3: Proportion Receiving Hearing, By Score Category ----
table3 <-
    rbind(c("", "Low Delegation", "High Delegation"),
          c("Low Discretion", round(mean(eo.df[eo.df$hidel == 0 & eo.df$hidis == 0,]$any.hearing), 3), round(mean(eo.df[eo.df$hidel == 1 & eo.df$hidis == 0,]$any.hearing), 3)),
          c("High Discretion", round(mean(eo.df[eo.df$hidel == 0 & eo.df$hidis == 1,]$any.hearing), 3), round(mean(eo.df[eo.df$hidel == 1 & eo.df$hidis == 1,]$any.hearing), 3)))


capture.output(stargazer(table3,
                         title = "Proportion Receiving Hearing, By Score Category",
                         type = 'text',
                         header = FALSE),
               file = "./table3.txt")

#Table 4: Baseline specifications ----

#Any hearing
b1 <- glm(any.hearing ~ hidel*hidis, data = eo.df, family = 'binomial')
b2 <- feglm(any.hearing ~ hidel*hidis | president, data = eo.df, family = 'binomial')
b3 <- feglm(any.hearing ~ hidel*hidis | year, data = eo.df, family = 'binomial')
b4 <- feglm(any.hearing ~ hidel*hidis | majortopic, data = eo.df, family = 'binomial')

#Total hearings
b5 <- glm.nb(n.hearings ~ hidel*hidis, data = eo.df, control = glm.control(maxit = 100))
b6 <- fenegbin(n.hearings ~ hidel*hidis | president, data = eo.df)
b7 <- fenegbin(n.hearings ~ hidel*hidis | year, data = eo.df)
b8 <- fenegbin(n.hearings ~ hidel*hidis | majortopic, data = eo.df)

#Table
fe.row <- data.frame(rbind(c("FE", "None", "President", "Year", "Policy", "None", "President", "Year", "Policy")))

modelsummary(list(b1, b2, b3, b4, b5, b6, b7, b8),
             stars = TRUE,
             output = "kableExtra",
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("hidel" = "Delegation", "hidis" = "Discretion", "hidel:hidis" = "Del. x Dis.", "(Intercept)" = "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Baseline Specifications")   %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./table4.png")

#Figure 1: Predicted Hearing Activity by Delegation and Discretion (Baseline) ----
fig1 <-
    rbind(
        ggeffect(b1, terms = c("hidel", "hidis")) %>%
            data.frame() %>%
            mutate(model = "Any Hearing (Logit)",
                   Delegation = as.factor(x),
                   Discretion = group),
        ggeffect(b5, terms = c("hidel", "hidis")) %>%
            data.frame() %>%
            mutate(model = "N Hearings (Neg. Binomial)",
                   Delegation = as.factor(x),
                   Discretion = group)
    )

ggsave(ggplot(fig1, aes(x = Delegation, y = predicted, shape = Discretion, color = Discretion)) +
           geom_point(position = position_dodge(width = 0.5)) +
           geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                         size = 0.4, width = 0.1,
                         position = position_dodge(width = 0.5)) +
           facet_wrap(~ model, scales = 'free') +
           theme_bw() +
           labs(y = "Predicted Outcome"),
       filename = "./fig1.png")

# Table 5: Full Specifications ----

#Any Hearing
f1 <- glm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df, family = 'binomial')
f2 <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | president, data = eocongress.df, family = 'binomial')
f3 <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df, family = 'binomial')
f4 <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df, family = 'binomial')

#Total Hearings
f5 <- glm.nb(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df, control = glm.control(maxit = 100))
f6 <- fenegbin(n.hearings ~ hidel*hidis + eo.age+ previous.hearings + chamber + divided.gov | president, data = eocongress.df)
f7 <- fenegbin(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df)
f8 <- fenegbin(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df)


#Table
modelsummary(list(f1, f2, f3, f4, f5, f6, f7, f8),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("hidel" = "Delegation", "hidis" = "Discretion", "hidel:hidis" = "Del. x Dis.", "eo.age" = "EO Age", "previous.hearings" = "Hearings", "chamberSenate" = "Senate", "divided.gov" = "Divided", "(Intercept)" =
                              "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Full Specifications")  %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./table5.png")



